<?php

use yii\helpers\Html;
use yii\grid\GridView;
use backend\models\Contractor;
use backend\helpers\ActionButton;
use backend\models\ContractStatus; 
use backend\helpers\FileViewHelper;
use kartik\select2\Select2;

/* @var $this yii\web\View */
/* @var $searchModel backend\models\ContractSearch */
/* @var $dataProvider yii\data\ActiveDataProvider */

$this->registerJs(
    "$('.js-file-exist').tooltip();",
);

$this->title = 'Мои договоры';
$this->params['breadcrumbs'][] = $this->title;
?>

<div class="container contract-index">

    <h1 class="main-title"><?= Html::encode($this->title) ?></h1>

</div>

<div class="container-fluid contract-table-container">

    <?= GridView::widget([
        'dataProvider' => $dataProvider,
        'filterModel' => $searchModel,
        'tableOptions' => [
            'class' => 'table table-striped'
        ],
        'columns' => [
            'own_number',
            //'contractor_number',
            [
                'attribute' => 'id_contractor',
                'filter' => Select2::widget([
                    'name' => 'contractor_in_grid',
                    'model' => $searchModel,
                    'data' => Contractor::contractorList(),
                    'attribute' => 'id_contractor',
                    'value' => 'contractor.name_short_opf',
                    'options' => [
                        'id' => 'select2-contractor__grid',
                        'class' => 'form-control form-control-sm',
                        'placeholder' => 'Выберите контрагента...'
                    ],
                    'pluginOptions' => [
                        'allowClear' => true
                    ],
                ]),
                'value' => 'contractor.name_short_opf',
            ],
            'subject',
            'total_price:currency',
            [
                'attribute' => 'date_sign',
                'filter' => Html::activeInput('date', $searchModel, 'date_sign', ['class' => 'form-control']),
                'format' => 'date',
            ],
            //'date_sign:date',
            //'date_realize:date',
            [
                'attribute' => 'date_realize',
                'filter' => Html::activeInput('date', $searchModel, 'date_realize', ['class' => 'form-control']),
                'format' => 'date',
            ],
            [
                'attribute' => 'id_status',
                'filter' => ContractStatus::find()->select(['title', 'id'])->indexBy('id')->column(),
                'filterInputOptions' => ['class' => 'form-control form-control-sm'],
                'value' => function($model) { 
                    return '<span class="badge ' . $model->status->css_style . '">' . $model->status->title . '</span>'; 
                },
                'format' => 'raw',
            ],
            [
                'label' => 'Файлы',
                'value' => function ($model) { return FileViewHelper::fileExist($model); },
                'format' => 'raw',
            ],
            [
                'class' => 'yii\grid\ActionColumn',
                'visibleButtons' =>
                [
                    'view' => Yii::$app->user->can('browseContract'),
                    'update' => Yii::$app->user->can('updateContract'),
                    'delete' => Yii::$app->user->can('deleteContract'),
                ]
            ],
        ],
    ]); ?>

</div>